# 发送邮箱任务
from django.conf import settings
from django.core.mail import send_mail

from celery_tasks.main import celery_app

from users.constants import RETRY_BACKOFF_NUM


# retry_backoff 错误重发次数3次
@celery_app.task(bind=True, name='send_verify_email', retry_backoff=RETRY_BACKOFF_NUM)
def send_verify_email(self,to_email, verify_url):
    """
    发送验证邮件
    :param to_email 目标邮箱
    :param verify_url 超链接地址
    """
    subject = "天天商城邮箱验证"
    html_message = '<p>尊敬的用户您好！</p>' \
                   '<p>感谢您使用天天商城。</p>' \
                   '<p>您的邮箱为：%s 。请点击此链接激活您的邮箱：</p>' \
                   '<p><a href="%s">%s<a></p>' % (to_email, verify_url, verify_url)
    try:
        send_mail(
            subject=subject,
            message="",
            from_email=settings.EMAIL_FROM,
            recipient_list=[to_email],
            html_message=html_message)
    except Exception as e:
        print(e)
        raise self.retry(exc=e, max=RETRY_BACKOFF_NUM)

